<?php
/**
 * 会员相关
 */

namespace App\Http\Controllers\Admin\Member;

use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class MemberController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取成员列表
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getMemberList(Request $request)
    {
        //验证规则
        $rules = [
            'member_name' => 'string|nullable',
            'email' => 'string|nullable',
            'mobile' => 'string|nullable',
            'create_time' => 'array|nullable',
            'update_time' => 'array|nullable'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getMemberList($validator->getData());
    }

    /**
     * 获取详情
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getMemberInfo(Request $request)
    {
        $rules = [
            'id' => 'required_without_all:uuid,token|int',
            'uuid' => 'required_without_all:id,token|string',
            'token' => 'required_without_all:id,uuid|string'
        ];
        $messages = [

        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getMemberInfo($validator->getData());

    }

    /**
     * 添加
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addMember(Request $request)
    {
        $rules = [
            'member_name' => 'required|string|unique:member,member_name',
            'email' => 'required|string',
            'mobile' => 'nullable|string',
            'password' => 'nullable|string',
        ];
        $messages = [
            'member_name.unique' => '用户名已存在!',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addMember($validator->getData());
    }

    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function delMember(Request $request)
    {
        $rules = [
            'ids' => 'required',
        ];
        $messages = [

        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delMember($validator->getData());
    }

    /**
     * 编辑
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function editMember(Request $request)
    {
        $rules = [
            'id' => 'required',
            'role_id' => 'nullable|integer',
            'member_name' => 'nullable|string',
            'email' => 'nullable|string',
            'mobile' => 'nullable|string',
            'password' => 'nullable|string',
        ];
        $messages = [

        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editMember($validator->getData());
    }

    public function loginOut(Request $request)
    {
        $rules = [
            'id' => 'required_without:token|string',
            'token' => 'required_without:id|string'
        ];
        $messages = [

        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->loginOut($validator->getData());
    }

    public function login(Request $request)
    {
        $rules = [
            'member_name' => 'required|string',
            'password' => 'required|string',
        ];
        $messages = [

        ];
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->login($validator->getData());
    }
}
